*********************************************************************
*This file cleans population data at the state level from Census/ACS*
*********************************************************************

foreach year in 1990 2000 2008 2016 {
use "$raw_data_lmarket/ipums_census.dta", clear

use if year==`year' using "$raw_data_lmarket/ipums_census.dta", clear

if `year'==1990 {
gen yearGroup = 1994
}
else if `year'==2000 {
gen yearGroup = 2000
}
else if `year'==2008 {
gen yearGroup = 2007
}
else if `year'==2016 {
gen yearGroup = 2014
}

drop if mi(yearGroup)

*Drop institutional group quarters* 
quietly: drop if gqtyped>=100 & gqtyped<=499

keep if age>=19&age<=64

*population
gen ipums_pop = 1

**Aggregate at state level**
collapse (sum) ipums_* [fw=perwt], by(statefip yearGroup) fast

save "$clean_data_lmarket/census_`year'_populationDataState.dta", replace
}

*Combine data
use "$clean_data_lmarket/census_1990_populationDataState.dta", clear
append using "$clean_data_lmarket/census_2000_populationDataState.dta"
append using "$clean_data_lmarket/census_2008_populationDataState.dta"
append using "$clean_data_lmarket/census_2016_populationDataState.dta"
save "$clean_data_lmarket/census_populationDataState.dta", replace

rm "$clean_data_lmarket/census_2000_populationDataState.dta"
rm "$clean_data_lmarket/census_2008_populationDataState.dta"
rm "$clean_data_lmarket/census_2016_populationDataState.dta" 

